// pages/get-reward/get-reward.js
import {Request} from '../../../utils/request.js';
const app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    myResult: {},
    isSubmit: false, // 信息时候提交
  },

  
    /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    if(prevPage.route != "pages/activity/win/win") {
      wx.switchTab({
        url: '/pages/index/index',
        success: function (res) {
        }
      })
    }else {
      let index = options.index;
      let myResult = prevPage.data.winList[index];
      that.setData({
        myResult: myResult,
        isSubmit: myResult.address_or_contact != null
      })
    }

   

    //that.onGetContact();


  },

  //获取地址
  handleChoiceAddress : function(){
    var that = this;
    if(that.data.myResult.overtime) { //过期
      wx.showToast({
        title: "领奖已过期，无法领取",
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    
		//1 获取权限状态
		wx.getSetting({
			success:(result)=>{
				//2 获取权限状态 当属性名比较怪异时，都要使用[]形式来获取属性值
				const scopeAddress = result.authSetting['scope.address']
				if(scopeAddress ==true ||scopeAddress ==undefined){
					wx.chooseAddress({
						success:(e)=>{
              if(e.errMsg == 'chooseAddress:ok') {

                var address = [];
                var contact = [];
                address.push(e.provinceName);
                address.push(e.cityName);
                address.push(e.countyName);
                address.push(e.detailInfo);
                contact.push(e.userName);
                contact.push(e.telNumber);

                var data = {
                  address: address.join(" "),
                  contact: contact.join(" "),
                }
                that.setData({
                  'myResult.address_or_contact': data
                });

                /*
                let param = {
                  url: 'user/address/post',
                  method: 'POST',
                  data: {
                    address_id: 0,
                    province: e.provinceName,
                    city: e.cityName,
                    detail: e.countyName + ' ' + e.detailInfo,
                    default: 0,
                    name: e.userName,
                    mobile: e.telNumber,
                  }
                }

                  Request(param).then(function(res){
                    
                    

                  });
                  */



    
              }
              // console.log(e);
              // console.log(data);

             // that.addUserContact(data);
              // var reward_info = that.data.reward_info;
              // reward_info.address = address.join(",");
              // reward_info.contact = contact.join(",");
              
						}
					})
				}else{
				//用户曾经取消过授权
					wx.openSetting({
						success:(e)=>{
						//用户从新授权后，再次调用获取收货地址
							wx.chooseAddress({
								success:(e)=>{
									console.log(e)
								}
							})
						}
					})
				}
			}
			
		})
  }, 
  

  getInpValue: function(e) {
    let that = this;
    let remark = e.detail.value;
    if(remark.length > 100) {
      wx.showToast({title: "字数已经超出限制",icon: 'none',duration: 1500 });
    }else {
      that.setData({
        'myResult.address_or_contact.remark': remark,
      });
    }
    

  },

  onConfirm: function() {
    let that = this;
    let address = that.data.myResult.address_or_contact;
    if(address == null || address == '') {
      return ;
    }
    let param = {
      url: 'activity/reward',
      method: 'POST',
      data: {
        activity_join_id: that.data.myResult.id,
        address: address,
      }
    }

    Request(param).then(function(res){
        that.setData({
          isSubmit: true
        })
    });
    
 

  },







  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  //预览二维码
  onImgPre: function() {
    var that = this;
    var src = that.data.myResult.prize.way.grcode;//获取data-src
    var imgList = [src];//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },

  onCopy: function(e) {

    var that = this;
    var copy = e.currentTarget.dataset.copy;

    wx.setClipboardData({
      data:copy,
      success(res) {
      }
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

})